package com.atxwq.shop.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atxwq.shop.manager.mapper.OrderInfoMapper;
import com.atxwq.shop.manager.mapper.OrderStatisticsMapper;
import com.atxwq.shop.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description 订单时间处理类
 * @Date 2025/5/16 12:53
 * @Created by 12336
 */
@Component
@Slf4j
public class OrderStatisticsTask {
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Scheduled(cron = "0 0 2 * * ?")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void orderTotalAmountStatistics(){
        //1.获取前一天日期
        String createDate = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");

        //2.统计前一天的订单金额
        OrderStatistics orderStatistics = orderInfoMapper.selectStatisticsByDate(createDate);

        //3.把统计后的数据添加到结果表
        if(orderStatistics != null){
            orderStatisticsMapper.insert(orderStatistics);
            log.info("{}订单统计数据添加成功", createDate);
        }
    }
    //  凌晨两点统计订单

}
